package org.batatasframework.common;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;

public class CurrencyMarker {
	private final static String CURRENCY = "￥";
	/** 货币单位 */
	private final static String CURRENCY_UNIT = "元";
//	/** 低阶数，一般不用到的 */
//	private final static String[] LOWER_LEVELS = { "十", "百", "千" };
	/** 高阶数 */
	private final static String[] HIGHER_LEVELS = { "", "万", "亿", "兆", "京", "无量数" };
	private final static int LOWER_PRECISIONS = 2;
	private final static int HIGHER_PRECISIONS = 4;
	/** 进位率，中国是10000，如：万、亿、 兆*/
	private final static int RATE = 10000;
	final static NumberFormat LOWER_FORMATER;
	final static NumberFormat HIGHER_FORMATER;
	
	static{
		LOWER_FORMATER = new DecimalFormat(formatString(LOWER_PRECISIONS));
		HIGHER_FORMATER = new DecimalFormat(formatString(HIGHER_PRECISIONS));
	}
//	
//	private final static CurrencyRate[] RATES = { 
//		new CurrencyRate(1, "元"), 
//		new CurrencyRate(10000, "万元"), 
//		new CurrencyRate(100000000, "亿元") 
//	};
//	
//	static class CurrencyRate{
//		
//		final long rate;
//		final String name;
//		final NumberFormat precision;
//		
//		CurrencyRate(long rate, String name, NumberFormat precision){
//			this.rate = rate;
//			this.name = name;
//			this.precision = precision;
//		}
//		
//		CurrencyRate(long rate, String name){
//			this(rate, name, HIGHER_FORMATER);
//		}
//		
//		String format(double value){
//			return "";
//		}
//	}
//	
	private final static String formatString(int precision){
		char[] cs = new char[precision + 2];
		cs[0] = '#';
		cs[1] = '.';
//		Arrays.fill(cs, 2, precision + 1, '0');
		Arrays.fill(cs, 2, precision + 2, '#');
		return new String(cs);
	}
	
	/** 创建一个货币处理的对象 */
	public static CurrencyMarker valueOf(Double value){
		return new CurrencyMarker(value);
	}
	
	/** 直接显示货币的格式，如：554.00元等格式 */
	public static String toString(Double value){
		return new CurrencyMarker(value).toString();
	}

	/** 直接对货币进行格式化处理 */
	public static String toFormat(Double value){
		return new CurrencyMarker(value).toFormat();
	}
	/** 千位，号分开 元为单位 */
	public static String toFormat2(Double value){
		DecimalFormat df = new DecimalFormat("#,###.00");
		return df.format(value)+CURRENCY_UNIT;
	}
	
	private double value;
	private String pre = "";
	private String unit = CURRENCY_UNIT;
	
	private CurrencyMarker(Double value){
		this.value = null == value  ? 0D : value;
	}
	
	public double getValue() {
		return value;
	}
	
	@Override
	public String toString() {
		return LOWER_FORMATER.format(value) + unit;
	}
	
	/** 进行格式化显示 */
	public String toFormat() {
		double value = this.value;
		int i = 0; 
		while(value > RATE && i < HIGHER_LEVELS.length){
			value /= RATE;
			i++;
		}
		
		String str = pre;
		if(i > 0){
			str += HIGHER_FORMATER.format(value);
		}else{
			str += LOWER_FORMATER.format(value);
		}
		str += HIGHER_LEVELS[i] + unit;
		
		return str;
	}
}
